package ma.techit.pfe.agence.voyage.dao.implementation;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ma.techit.pfe.agence.voyage.dao.interfaces.AdministrateurDaoIn;
import ma.techit.pfe.agence.voyage.model.Administrateur;
import ma.techit.pfe.agence.voyage.util.SessionFactoryHelper;

@Repository
public class AdministrateurDao extends SessionFactoryHelper implements AdministrateurDaoIn {

	public void save(Administrateur admin) {
		// TODO Auto-generated method stub
		getCurrentSession().save(admin);
		
	}

	public void update(Administrateur admin) {
		// TODO Auto-generated method stub
		getCurrentSession().update(admin);
		
	}

	public void delete(Long id) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(findByID(id));
	}

	public Administrateur findByID(Long id) {
		// TODO Auto-generated method stub
		return (Administrateur) getCurrentSession()
				.createQuery("from Administrateur  where id = " + id).list()
				.get(0);
	}

	public List<Administrateur> findAll() {
		// TODO Auto-generated method stub
		return  getCurrentSession()
				.createQuery("from Administrateur ").list()
				;
	}

	public Administrateur authenticate(String login, String password) {
		// TODO Auto-generated method stub
		List fetchedUsers = getCurrentSession().createQuery(
				"from Administrateur where login ='" + login + "' and password ='"
						+ password + "'").list();
		return fetchedUsers.isEmpty() ? null : (Administrateur) fetchedUsers.get(0);
	}

	}

